<?php

/**
 * QPHP
 *
 * Copyright (c) 2022 QPHP
 *
 * @author lbnnbs
 * @copyright Copyright (c) 2022 lbnnbs QPHP (https://gitee.com/lbnnbs/qphp)
 * @license AGPLv3 License
 * 
 * 分页工具
 */
class Page {

    /**
     * 请求组件
     *
     * @var object
     */
    private $_request = null;

    /**
     * 属性设置
     *
     * @var array
     */
    private $_options = array
        (
        // 总记录数
        'count' => 100,
        // 每页显示多少行记录
        'perpage' => 10,
        // 显示多少个分页导航按钮
        'navnum' => 10,
        // 分页的URL,为空时则自动使用当前的URL
        'url' => '',
        // 页号用的参数名,如果与现有的参数有冲突可以改变这个值
        'tagname' => 'page',
        // '上一页' 显示的字符
        'prev' => '&lsaquo;&lsaquo;',
        // '下一页' 显示的字符
        'next' => '&rsaquo;&rsaquo;',
    );

    /**
     * 构造函数
     *
     */
    public function __construct() {
        $this->_request = Request::getInstance();
    }

    /**
     * 设置属性
     *
     * @param array $options 属性设置
     */
    public function set($options) {
        $this->_options = array_merge($this->_options, $options);
        return $this;
    }

    public function get($option) {
        return isset($this->_options[$option]) ? $this->_options[$option] : null;
    }

    /**
     * 返回分页信息
     *
     * @return array
     */
    public function result() {
        // 当前第几页
        $curpage = $this->_getCurPage();

        // 解析当前的URL
        $this->_parseUrl();

        $realpages = 1;
        $pages = 0;
        if ($this->_options['count'] > $this->_options['perpage']) {
            $offset = 2;
            $pages = @ceil($this->_options['count'] / $this->_options['perpage']);

            if ($this->_options['navnum'] > $pages) {
                $from = 1;
                $to = $pages;
            } else {
                $from = $curpage - $offset;
                $to = $from + $this->_options['navnum'] - 1;
                if ($from < 1) {
                    $to = $curpage + 1 - $from;
                    $from = 1;
                    if ($to - $from < $this->_options['navnum']) {
                        $to = $this->_options['navnum'];
                    }
                } elseif ($to > $pages) {
                    $from = $pages - $this->_options['navnum'] + 1;
                    $to = $pages;
                }
            }

            // 获取各个页面地址
            $urls = array();
            if ($curpage - $offset > 1 && $pages > $this->_options['navnum']) {
                $urls['first'] = array('text' => '&laquo;', 'url' => $this->_options['url'] . '1'); // 首页
            }

            if ($curpage > 1) {
                $urls['prev'] = array('text' => '&lsaquo;', 'url' => $this->_options['url'] . ($curpage - 1)); // 上一页
            }

            if ($curpage < $pages) {
                $urls['next'] = array('text' => '&rsaquo;', 'url' => $this->_options['url'] . ($curpage + 1)); // 下一页
            }

            if ($to < $pages) {
                $urls['last'] = array('text' => '&raquo;', 'url' => $this->_options['url'] . $pages); // 最后一页
            }

            for ($i = $from; $i <= $to; $i++) {
                if ($i == $curpage) {
                    $urls['pages'][] = array('text' => $i, 'url' => 'javascript:void(0);');
                } else {
                    $urls['pages'][] = array('text' => $i, 'url' => $this->_options['url'] . $i);
                }
            }
        }
        return array(
            // 总页数
            'pagenum' => $pages,
            // 当前页
            'curpage' => $curpage,
            // 各个页面地址
            'urls' => $urls,
        );
    }

    /* ------------------------ 以下私有方法 ---------------------- */

    /**
     * 得到当前所是第几页
     *
     * @return int
     */
    private function _getCurPage() {
        $pagenum = $this->_request->get($this->_options['tagname']);
        return max($pagenum, 1);
    }

    /**
     * 解析URL
     *
     * @return void
     */
    private function _parseUrl() {
        // 得到 URL
        $url = $this->_options['url'] ? $this->_options['url'] : $this->_request->currentURL();
        // 分页的参数名
        $tagname = $this->_options['tagname'];
        $pattern = '/[\&]*' . $tagname . '[=|\d]*/';
        $url = preg_replace($pattern, '', $url);
        $url .= strpos($url, '?') ? '&amp;' : '?';
        $url .= $tagname . '=';
        $this->_options['url'] = $url;
    }

}
